import { request } from '@/axios/request'
import type { ArticleVO, AttitudeVO } from '@/types/article'

/**
 * 文章相关API服务
 * 提供可复用的API接口方法
 */
export class ArticleService {
  /**
   * 获取文章列表
   */
  static async getArticleList(): Promise<ArticleVO[]> {
    const res = await request('/api/article/list')
    return res.data || []
  }

  /**
   * 根据ID查询文章详情（用于展示）
   */
  static async getArticleById(articleId: string, includeAttitude = true): Promise<any> {
    const params = { 
      id: articleId, 
      type: includeAttitude ? 'full' : 'noAttitude' 
    }
    const res = await request('/api/article/query', params)
    return res.data
  }

  /**
   * 根据ID查询文章详情（用于编辑）
   */
  static async getArticleForEdit(articleId: string): Promise<ArticleVO> {
    const res = await request('/api/article/queryForEdit', { id: articleId })
    return res.data
  }

  /**
   * 保存草稿
   */
  static async saveDraft(articleData: Partial<ArticleVO>): Promise<any> {
    const res = await request('/api/article/saveOrPublish', articleData)
    return res.data
  }

  /**
   * 发布文章
   */
  static async publishArticle(articleData: Partial<ArticleVO>): Promise<any> {
    const res = await request('/api/article/saveOrPublish', articleData)
    return res.code
  }

  /**
   * 更新文章
   */
  static async updateArticle(articleData: Partial<ArticleVO>): Promise<any> {
    const res = await request('/api/article/update', articleData)
    return res.data
  }

  /**
   * 删除文章
   */
  static async deleteArticle(articleId: string): Promise<any> {
    const res = await request('/api/article/delete', { id: articleId })
    return res.data
  }

  /**
   * 展示对文章的态度（点赞、收藏等）
   */
  static async showAttitude(attitudeData: AttitudeVO): Promise<any> {
    const res = await request('/api/article/showAttitude', attitudeData)
    return res.data
  }

  /**
   * 上传文章封面
   */
  static async uploadCover(file: File): Promise<string> {
    const formData = new FormData()
    formData.append('file', file)
    const res = await request('/api/upload/cover', formData, 'POST')
    return res.data?.url || ''
  }

  /**
   * 获取文章分类列表
   */
  static async getArticleTypes(): Promise<any[]> {
    const res = await request('/api/article/types')
    return res.data || []
  }

  /**
   * 搜索文章
   */
  static async searchArticles(keyword: string, filters?: any): Promise<ArticleVO[]> {
    const params = { keyword, ...filters }
    const res = await request('/api/article/search', params)
    return res.data || []
  }
}